/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package descriptorarchivos;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Ascalero
 */
public class DescriptorArchivos implements Serializable {

    private ArrayList <String> nomTablas= new ArrayList<>();
    
private  boolean existTabla(String nom){
        if(nomTablas.isEmpty()){
        return true;
        }else{
            for(String a:nomTablas){
                if (a.compareTo(nom)==0)
                return false;
            }
            return true;
        }
    }
public void nuevaTabla(){
        
        Scanner in= new Scanner(System.in);
        System.out.println("Como se llamara la tabla");
        String nomtabla= in.nextLine();
        while(!existTabla(nomtabla)){
            System.out.println("Ese Nombre de Tabla ya existe, ponga uno nuevo");
            System.out.println("Como se llamara la tabla");
            nomtabla= in.nextLine();
            }
        System.out.println("Cuantos Campos tendra?");
        int numcampos= in.nextInt();
        Tabla temp= new Tabla(nomtabla,numcampos);
        temp.guardar();
        nomTablas.add(nomtabla);
        saveDesc();
      }

public void tablasExistentes(){
int i=0;
    System.out.println("Las tablas guardadas son");
    for (String name:nomTablas){
       System.out.println(i+".-"+name);
       i++;
    }
}

public void VerTabla(){
    int i=0;
    System.out.println("Que tabla desea ver");
    for (String name:nomTablas){
       System.out.println(i+".-"+name);
       i++;
    }
    System.out.println("Selecciona el NUmero");
    Scanner in= new Scanner(System.in);
    String strinTemp= nomTablas.get(in.nextInt());
    Tabla temp= getTabla(strinTemp);
        temp.displayContent();
}

public Tabla getTabla(String nom){
        Tabla recuperada= new Tabla();
        try{
        nom=nom+".tab";
        FileInputStream fi= new FileInputStream(nom);
        ObjectInputStream os=new ObjectInputStream(fi);
        recuperada=(Tabla)os.readObject();
        os.close();
        }catch (ClassNotFoundException ex) {
            Logger.getLogger(Tabla.class.getName()).log(Level.SEVERE, null, ex);
        }catch(IOException e){
            e.printStackTrace();
        }
        return recuperada;
                
    }

public static DescriptorArchivos getDesc()throws IOException,ClassNotFoundException{
        DescriptorArchivos recuperada= new DescriptorArchivos();
        
        FileInputStream fi= new FileInputStream("desc.int");
        ObjectInputStream os=new ObjectInputStream(fi);
        recuperada=(DescriptorArchivos)os.readObject();
        os.close();
        return recuperada;
    }

public void saveDesc(){
        try{
            
            FileOutputStream fs= new FileOutputStream("desc.int");
            ObjectOutputStream os= new ObjectOutputStream(fs);
            os.writeObject(this);
            os.close();
        }catch(IOException e){
            e.printStackTrace();
        }
        }
//// aki se ponen las operaciones
    public Tabla[] getTablas(){
        Tabla a,b,c[]= new Tabla[2];
        int i=0;
            System.out.println("Selecciona la Tabla 1");
               for (String name:nomTablas){
                System.out.println(i+".-"+name);
                i++;
            }
            System.out.println("Selecciona el Numero");
            Scanner in= new Scanner(System.in);
            String strinTemp= nomTablas.get(in.nextInt());
            a= getTabla(strinTemp);
            c[0]=a;
            i=0;
            System.out.println("Selecciona la Tabla 2");
               for (String name:nomTablas){
                System.out.println(i+".-"+name);
                i++;
            }
            System.out.println("Selecciona el Numero");
            in= new Scanner(System.in);
            strinTemp= nomTablas.get(in.nextInt());
            b= getTabla(strinTemp);
            c[1]=b;
            return c; 
    }
    public boolean cartesiano(){
        Tabla a,b,c[];
            c=getTablas();
            a=c[0];
            b=c[1];
            if (a.getSize()!=b.getSize())return false;
            ArrayList<Tupla> aVal=a.getValores();
            ArrayList<Tupla> bVal=b.getValores();
                auxDisplayNFields(a.getNAttrib(),b.getNAttrib());
            for (Tupla t1:aVal){
                for(Tupla t2: bVal){
                    auxDisplay(t1.getCampos(),t2.getCampos());
                }
            }
            return true;
      }
    
    
    public boolean union(){
        Tabla a,b,c[],union;
            c=getTablas();
            
            a=c[0];
            b=c[1];
            if (a.getSize()!=b.getSize())return false;
            ArrayList<Tupla> aVal=a.getValores();
            ArrayList<Tupla> bVal=b.getValores();
                union= new Tabla(a.getSize());
                String[] zip= new String[a.getSize()];
                for(int x=0;x<a.getSize();x++){
                zip[x]="a"+x;
                }
                union.setnomAttrib(zip);
                boolean found;
            for (Tupla t1:aVal){
                union.addTupla(t1.getCampos());
            }
            for (Tupla t2:bVal){
                found=false;
                for(Tupla t1: aVal){
                    if(t2.compCon(t1.getCampos()))found=true;
                }
                if(!found)union.addTupla(t2.getCampos());
            }
            union.displayContent();
            
            return true;
      }
    public boolean diferencia(){
        Tabla a,b,c[],dif;
            c=getTablas();
            
            a=c[0];
            b=c[1];
            if (a.getSize()!=b.getSize())return false;
            ArrayList<Tupla> aVal=a.getValores();
            ArrayList<Tupla> bVal=b.getValores();
                dif= new Tabla(a.getSize());
                String[] zip= new String[a.getSize()];
                for(int x=0;x<a.getSize();x++){
                zip[x]="a"+x;
                }
                dif.setnomAttrib(zip);
                boolean found;
            for (Tupla t1:aVal){
                found=false;
                for(Tupla t2: bVal){
                    if(t1.compCon(t2.getCampos()))found=true;
                }
                if(!found)dif.addTupla(t1.getCampos());
            }
            
            dif.displayContent();
            
            return true;
      }
    
    
        private void auxDisplayNFields(String[] a,String[] b){
             int i;
             for(i=0;i<a.length;i++)
             System.out.print(i+".-"+a[i]+"   ");
             for(int j=0;j<b.length;j++){
             System.out.print(i+".-"+b[j]+"   ");
             i++;
             }
             System.out.println("");
        }
        private void auxDisplay(String[] a,String[] b){
             int i;
             for(i=0;i<a.length;i++)
             System.out.print(a[i]+"   ");
             for(int j=0;j<b.length;j++){
             System.out.print(b[j]+"   ");
             i++;
             }
             System.out.println("");
        }
 public void modificaTabla(){
            int i=0;
    System.out.println("Que tabla desea modificar");
    for (String name:nomTablas){
       System.out.println(i+".-"+name);
       i++;
    }
    System.out.println("Selecciona el NUmero");
    Scanner in= new Scanner(System.in);
    String strinTemp= nomTablas.get(in.nextInt());
    Tabla temp= getTabla(strinTemp);
            boolean flag=true;
            while(flag){
                System.out.println("Que desea Hacer");
                System.out.println("1.-Agregar Una Tupla");
                System.out.println("2.-regresar al menu");
            switch(in.nextInt()){
                case 1:temp.addTupla();
                    temp.guardar();
                break;
                case 2: flag=false; break;
                default: flag=false; break;
            }
            }
        
        }
        
}
